package teshop.web.action.product;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import teshop.data.domain.Product;
import teshop.service.CatalogService;


@SuppressWarnings("serial")
@Component("newProduct")
public class NewProduct implements Serializable {
	
	private CatalogService service;
	
	private Product product;
	
	@Autowired
	public NewProduct(CatalogService service) {
		this.service = service;
	}
	
	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	public String save() {
		service.addProduct(product);
		
		printMessage();
		
		return null;
	}
	
	private void printMessage() {
		FacesMessage facesMessage = new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Movie is saved successfully", "OK");
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
		
		product = new Product();
	}

}
